﻿
namespace NetROS.Tiers.Windows.Forms
{
	/// <summary>
	/// abstract ReportPivots typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class ReportPivotsDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<ReportPivotsDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.ReportPivots _currentReportPivots = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxReportPivotsDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxReportPivotsErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxReportPivotsBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the ID property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIDDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IDReport property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDReportDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the PrikazniNaziv property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxPrikazniNazivDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the PivotClass property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxPivotClassDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.Report> _IDReportList;
		
		/// <summary> 
		/// The list of selectable Report
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Report> IDReportList
		{
			get {return this._IDReportList;}
			set 
			{
				this._IDReportList = value;
				this.uxIDReportDataGridViewColumn.DataSource = null;
				this.uxIDReportDataGridViewColumn.DataSource = this._IDReportList;
			}
		}
		
		private bool _allowNewItemInIDReportList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Report
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDReportList
		{
			get { return _allowNewItemInIDReportList;}
			set
			{
				this._allowNewItemInIDReportList = value;
				this.uxIDReportDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.ReportPivots> _ReportPivotsList;
				
		/// <summary> 
		/// The list of ReportPivots to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.ReportPivots> ReportPivotsList
		{
			get {return this._ReportPivotsList;}
			set
			{
				this._ReportPivotsList = value;
				this.uxReportPivotsBindingSource.DataSource = null;
				this.uxReportPivotsBindingSource.DataSource = value;
				this.uxReportPivotsDataGridView.DataSource = null;
				this.uxReportPivotsDataGridView.DataSource = this.uxReportPivotsBindingSource;				
				//this.uxReportPivotsBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxReportPivotsBindingSource_ListChanged);
				this.uxReportPivotsBindingSource.CurrentItemChanged += new System.EventHandler(OnReportPivotsBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnReportPivotsBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentReportPivots = uxReportPivotsBindingSource.Current as Entities.ReportPivots;
			
			if (_currentReportPivots != null)
			{
				_currentReportPivots.Validate();
			}
			//_ReportPivots.Validate();
			OnCurrentEntityChanged();
		}

		//void uxReportPivotsBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.ReportPivots"/> instance.
		/// </summary>
		public Entities.ReportPivots SelectedReportPivots
		{
			get {return this._currentReportPivots;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxReportPivotsDataGridView.VirtualMode;}
			set
			{
				this.uxReportPivotsDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxReportPivotsDataGridView.AllowUserToAddRows;}
			set {this.uxReportPivotsDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxReportPivotsDataGridView.AllowUserToDeleteRows;}
			set {this.uxReportPivotsDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxReportPivotsDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxReportPivotsDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="ReportPivotsDataGridViewBase"/> class.
		/// </summary>
		public ReportPivotsDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxReportPivotsDataGridView = new System.Windows.Forms.DataGridView();
			this.uxReportPivotsBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxReportPivotsErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIDDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIDReportDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxPrikazniNazivDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxPivotClassDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			//this.uxIDReportBindingSource = new ReportBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDReportBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxReportPivotsDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxReportPivotsBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxReportPivotsErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxReportPivotsErrorProvider
			// 
			this.uxReportPivotsErrorProvider.ContainerControl = this;
			this.uxReportPivotsErrorProvider.DataSource = this.uxReportPivotsBindingSource;						
			// 
			// uxReportPivotsDataGridView
			// 
			this.uxReportPivotsDataGridView.AutoGenerateColumns = false;
			this.uxReportPivotsDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxReportPivotsDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIDDataGridViewColumn,
		this.uxIDReportDataGridViewColumn,
		this.uxPrikazniNazivDataGridViewColumn,
		this.uxPivotClassDataGridViewColumn			});
			this.uxReportPivotsDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxReportPivotsDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxReportPivotsDataGridView.Name = "uxReportPivotsDataGridView";
			this.uxReportPivotsDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxReportPivotsDataGridView.TabIndex = 0;	
			this.uxReportPivotsDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxReportPivotsDataGridView.EnableHeadersVisualStyles = false;
			this.uxReportPivotsDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnReportPivotsDataGridViewDataError);
			this.uxReportPivotsDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnReportPivotsDataGridViewCellValueNeeded);
			this.uxReportPivotsDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnReportPivotsDataGridViewCellValuePushed);
			
			//
			// uxIDDataGridViewColumn
			//
			this.uxIDDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDDataGridViewColumn.DataPropertyName = "ID";
			this.uxIDDataGridViewColumn.HeaderText = "ID";
			this.uxIDDataGridViewColumn.Name = "uxIDDataGridViewColumn";
			this.uxIDDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDDataGridViewColumn.ReadOnly = true;		
			//
			// uxIDReportDataGridViewColumn
			//
			this.uxIDReportDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDReportDataGridViewColumn.DataPropertyName = "IDReport";
			this.uxIDReportDataGridViewColumn.HeaderText = "IDReport";
			this.uxIDReportDataGridViewColumn.Name = "uxIDReportDataGridViewColumn";
			this.uxIDReportDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDReportDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDReportDataGridViewColumn.ReadOnly = false;		
			//
			// uxPrikazniNazivDataGridViewColumn
			//
			this.uxPrikazniNazivDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxPrikazniNazivDataGridViewColumn.DataPropertyName = "PrikazniNaziv";
			this.uxPrikazniNazivDataGridViewColumn.HeaderText = "PrikazniNaziv";
			this.uxPrikazniNazivDataGridViewColumn.Name = "uxPrikazniNazivDataGridViewColumn";
			this.uxPrikazniNazivDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxPrikazniNazivDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxPrikazniNazivDataGridViewColumn.ReadOnly = false;		
			//
			// uxPivotClassDataGridViewColumn
			//
			this.uxPivotClassDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxPivotClassDataGridViewColumn.DataPropertyName = "PivotClass";
			this.uxPivotClassDataGridViewColumn.HeaderText = "PivotClass";
			this.uxPivotClassDataGridViewColumn.Name = "uxPivotClassDataGridViewColumn";
			this.uxPivotClassDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxPivotClassDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxPivotClassDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDReportDataGridViewColumn
			//				
			this.uxIDReportDataGridViewColumn.DisplayMember = "Naziv";	
			this.uxIDReportDataGridViewColumn.ValueMember = "ID";	
			this.uxIDReportDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDReportDataGridViewColumn.DataSource = uxIDReportBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxReportPivotsDataGridView);
			this.Name = "ReportPivotsDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDReportBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxReportPivotsErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxReportPivotsDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxReportPivotsBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				ReportPivotsDataGridViewEventArgs args = new ReportPivotsDataGridViewEventArgs();
				args.ReportPivots = _currentReportPivots;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class ReportPivotsDataGridViewEventArgs : System.EventArgs
		{
			private Entities.ReportPivots	_ReportPivots;
	
			/// <summary>
			/// the  Entities.ReportPivots instance.
			/// </summary>
			public Entities.ReportPivots ReportPivots
			{
				get { return _ReportPivots; }
				set { _ReportPivots = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxReportPivotsDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnReportPivotsDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxReportPivotsDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnReportPivotsDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxReportPivotsDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						e.Value = ReportPivotsList[e.RowIndex].ID;
						break;
					case "uxIDReportDataGridViewColumn":
						e.Value = ReportPivotsList[e.RowIndex].IDReport;
						break;
					case "uxPrikazniNazivDataGridViewColumn":
						e.Value = ReportPivotsList[e.RowIndex].PrikazniNaziv;
						break;
					case "uxPivotClassDataGridViewColumn":
						e.Value = ReportPivotsList[e.RowIndex].PivotClass;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxReportPivotsDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnReportPivotsDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxReportPivotsDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						ReportPivotsList[e.RowIndex].ID = (System.Int32)e.Value;
						break;
					case "uxIDReportDataGridViewColumn":
						ReportPivotsList[e.RowIndex].IDReport = (System.Int32)e.Value;
						break;
					case "uxPrikazniNazivDataGridViewColumn":
						ReportPivotsList[e.RowIndex].PrikazniNaziv = (System.String)e.Value;
						break;
					case "uxPivotClassDataGridViewColumn":
						ReportPivotsList[e.RowIndex].PivotClass = (System.Byte[])e.Value;
						break;
				default:
				break;
			}
		}
	}
}
